function convert(str) {
  let len = str.length;
  let count = 0;
  let s = ''
  for (let i = len - 1; i >= 0; i--) {
    s = s + str[i];
    if (++count === 3) {
      s = s + ',';
      count = 0;
    }
  }
  let res = ''
  for (let i = s.length - 1; i >= 0; i--) {
    res = res + s[i]
  }
  if (res[0] === ',') {
    return res.slice(1)
  }
  return res;
}

console.log(convert("123"));